﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using RobotRace.engine.robossConnector;
using System.Text.RegularExpressions;

namespace RobotRaceGUI
{
    public partial class ConnectForm : Form
    {
        public RobossConnector connector { get; set; }

        public ConnectForm()
        {
            InitializeComponent();
            IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
            Regex ipAddressRegex = new Regex("\\d{0,3}(\\.\\d{0,3}){3}");
            foreach (IPAddress address in addresses)
            {
                if (ipAddressRegex.IsMatch(address.ToString()))
                {
                    ip.Text = address.ToString();
                }
            }
            
        }

        private void connectButton_Click(object sender, EventArgs e)
        {
            string ipAddress = ip.Text;
            string portNumber = port.Text;
            try
            {
                connector = RobossConnector.getConnector(ipAddress, portNumber);
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to connect! " + ex.Message);
                DialogResult = DialogResult.None;
            }
            
        }
    }
}
